package com.zyiot.lbs;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.waps.AdView;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.cloud.Bounds;
import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationOverlay;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.map.PopupClickListener;
import com.baidu.mapapi.map.PopupOverlay;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.umeng.analytics.MobclickAgent;
import com.zyiot.bdapi.StartNavi;
import com.zyiot.model.Eshop;
import com.zyiot.utils.ZyUnit;

public class Test_TabListMapActivity extends Activity {

	MapView mMapView;
	private static Context context;
	private boolean initSearchFlag = false;
	BMapManager mBMapManager = null;
	private MapController mMapController = null;
	private MyOverlay mOverlay = null;

	private InitApplication app = null;
	private List<Eshop> list = new ArrayList<Eshop>();

	private String region_name;

	GeoPoint leftBottom;
	GeoPoint rightTop;
	Bounds bounds;
	private Handler load_Handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		app = (InitApplication) this.getApplication();
		super.onCreate(savedInstanceState);

		context = this;
		setContentView(R.layout.test_layout_eshop_map);
		initMapView();
		// addAllMarker();
		popHandler.postDelayed(popRunnable, 500);
		Button btnList = (Button) findViewById(R.id.btnlist);
		btnList.setOnClickListener(btn_list_listener);

		// 互动广告调用方式
		LinearLayout container = (LinearLayout) findViewById(R.id.AdLinearLayout);
		new AdView(this, container).DisplayAd();

	}

	// 启动popwindow
	private Handler popHandler = new Handler();
	private Runnable popRunnable = new Runnable() {
		@Override
		public void run() {
			addAllMarker();
		}
	};

	private OnClickListener btn_list_listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			finish();
		}
	};

	@Override
	protected void onPause() {
		mMapView.onPause();
		super.onPause();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onResume() {
		mMapView.onResume();
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onDestroy() {
		mMapView.destroy();
		super.onDestroy();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mMapView.onSaveInstanceState(outState);

	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		mMapView.onRestoreInstanceState(savedInstanceState);
	}

	private void initMapView() {
		mMapView = (MapView) findViewById(R.id.bmapView);
		mMapView.setBuiltInZoomControls(true);
		// 设置启用内置的缩放控件
		MapController mMapController = mMapView.getController();
		// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
		GeoPoint point = new GeoPoint((int) (31.925 * 1E6), (int) (120.292 * 1E6));
		// 用给定的经纬度构造一个GeoPoint，单位是微度 (度 * 1E6)
		mMapController.setCenter(point);// 设置地图中心点
		mMapController.setZoom(14);// 设置地图zoom级别

	}

	/**
	 * 删除所有标记
	 */
	public void removeAllMarker() {
		mOverlay.removeAll();
		mMapView.refresh();
	}

	/**
	 * 添加所有标记
	 */
	public void addAllMarker() {
		// initApplication app = (initApplication) getApplication();
		list = app.getList();
		mOverlay = new MyOverlay(getResources().getDrawable(R.drawable.icon_mark), mMapView);
		for (Eshop content : list) {
			int latitude = (int) (content.getLatitude() * 1E6);
			int longitude = (int) (content.getLongitude() * 1E6);
			Drawable d = getResources().getDrawable(R.drawable.icon_mark);
			OverlayItem item = new OverlayItem(new GeoPoint(latitude, longitude), content.getName(), content.getAddr());
			item.setMarker(d);
			mOverlay.addItem(item);
		}

		mMapView.getOverlays().add(mOverlay);
		mMapView.refresh();

		// 如果定位成功，添加当前坐标点
		if (app.currlocation != null) {
			MyLocationOverlay myLocationOverlay = new MyLocationOverlay(mMapView);
			LocationData locData = new LocationData();
			// 手动将位置源置为天安门，在实际应用中，请使用百度定位SDK获取位置信息，要在SDK中显示一个位置，需要
			// 使用百度经纬度坐标（bd09ll）
			locData.latitude = app.currlocation.getLatitude();
			locData.longitude = app.currlocation.getLongitude();
			// 如果不显示定位精度圈，将accuracy赋值为0即可
			locData.accuracy = app.currlocation.getRadius();
			locData.direction = app.currlocation.getDerect();

			locData.direction = 2.0f;
			myLocationOverlay.setData(locData);
			mMapView.getOverlays().add(myLocationOverlay);
			mMapView.refresh();
		}

		// 江阴市政府的中心，无定位时的地图中心
		int cLat = 31925816;
		int cLon = 120292073;
		if (app.currlocation == null) {
			mMapView.getController().setCenter(new GeoPoint(cLat, cLon));
		} else //if (list != null && list.size() >= 1) {
		{
			//ContentModel c = (ContentModel) list.get(0);
			int currLat = (int) (app.currlocation.getLatitude() * 1E6);
			int currLon = (int) (app.currlocation.getLongitude() * 1E6);
			mMapView.getController().setCenter(new GeoPoint(currLat, currLon));
		}
	}

	/**
	 * 地图覆盖物，用于显示标记
	 */
	private int clickedTapIndex = -1;

	public class MyOverlay extends ItemizedOverlay {
		PopupOverlay pop = null;

		public MyOverlay(Drawable defaultMarker, MapView mapView) {
			super(defaultMarker, mapView);
			pop = new PopupOverlay(mMapView, new PopupClickListener() {
				@Override
				public void onClickedPopup(int index) {
					int endlat = (int) (list.get(clickedTapIndex).getLatitude() * 1E6);
					int endlong = (int) (list.get(clickedTapIndex).getLongitude() * 1E6);
//					Log.d("TEST", "TEST_endlong_" + endlat + "_endlong_" + endlong);
					GeoPoint endPoint = new GeoPoint(endlat, endlong);
					if (app.currlocation != null) {
						int startlat = (int) (app.currlocation.getLatitude() * 1E6);
						int startlong = (int) (app.currlocation.getLongitude() * 1E6);
						GeoPoint startPoint = new GeoPoint(startlat, startlong);
						StartNavi.startNavi(context, Test_TabListMapActivity.this, startPoint, endPoint);
					}
				}

			});
		}

		@Override
		protected boolean onTap(int index) {
			final List<Eshop> list = InitApplication.getInstance().getList();
			OverlayItem item = getItem(index);
			clickedTapIndex = index;
			if (index >= list.size()) {
				// 点击自己位置marker，不做任何处理
				return true;
			}
			View popview = LayoutInflater.from(context).inflate(R.layout.marker_pop, null);
			TextView tveshop_name = (TextView) popview.findViewById(R.id.tv_pop_eshop_name);
			TextView tveshop_address = (TextView) popview.findViewById(R.id.tv_pop_eshop_address);

			String eshop_name = list.get(index).getName();
			String eshop_addres = list.get(index).getAddr();
			tveshop_name.setText(eshop_name);
			tveshop_address.setText(eshop_addres);
			pop.showPopup(ZyUnit.getBitmapFromView(popview), item.getPoint(), 50);

			super.onTap(index);
			return false;
		}

		@Override
		public boolean onTap(GeoPoint pt, MapView mapView) {
			if (pop != null) {
				pop.hidePop();
			}
			super.onTap(pt, mapView);
			return false;
		}

	}

}
